package org.example.memora.controller;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.example.memora.DTO.CommentSaveDTO;
import org.example.memora.common.result.PageResult;
import org.example.memora.common.result.Result;
import org.example.memora.common.utils.UserHolder;
import org.example.memora.entity.Comment;
import org.example.memora.service.CommentService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/comments")
@Tag(name = "评论管理", description = "提供评论的新增、查询、删除等接口")
public class CommentController {

    private final CommentService commentService;
    public CommentController(CommentService commentService) {
        this.commentService = commentService;
    }

    @Operation(summary = "创建评论", description = "提交一条评论信息到数据库中")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "评论创建成功"),
            @ApiResponse(responseCode = "400", description = "请求数据格式错误"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @PostMapping
    public Result<?> save(@RequestBody CommentSaveDTO commentSaveDTO) {
        log.debug("开始创建评论: {}", commentSaveDTO);
        return commentService.save(commentSaveDTO);
    }

    @Operation(summary = "根据ID查询评论", description = "根据评论ID查询具体评论信息")
    @Parameter(name = "id", description = "评论ID", required = true)
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "评论查询成功"),
            @ApiResponse(responseCode = "400", description = "请求参数错误"),
            @ApiResponse(responseCode = "404", description = "未找到该评论"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping("/{id}")
    public Result<?> findById(@PathVariable String id) {
        log.info("查询评论，ID={}", id);
        return commentService.findById(id);
    }

    @Operation(summary = "查询所有评论", description = "返回数据库中所有的评论信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping
    public Result findAll() {
        log.debug("开始查询所有评论");
        return commentService.findAll();
    }

    @Operation(summary = "根据ID删除评论", description = "删除指定ID的评论")
    @Parameter(name = "id", description = "评论ID", required = true)
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "删除成功"),
            @ApiResponse(responseCode = "400", description = "请求参数错误"),
            @ApiResponse(responseCode = "404", description = "未找到该评论"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable String id) {
        log.debug("删除评论，ID={}", id);
        return commentService.deleteById(id);
    }

    @Operation(summary = "根据文章ID分页查询评论", description = "根据传入的 articleId，分页返回该文章下的评论列表")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "400", description = "参数错误"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })

    @GetMapping("/article/{articleId}")
    public Result<PageResult> getCommentsByArticleId(
            @Parameter(description = "文章ID", required = true)
            @PathVariable String articleId,

            @Parameter(description = "页码（从0开始）", example = "0")
            @RequestParam(defaultValue = "0") int page,

            @Parameter(description = "每页大小", example = "10")
            @RequestParam(defaultValue = "10") int size) {

        PageResult result = commentService.getCommentsByArticleId(articleId, page, size);
        return Result.success(result);
    }
    @Operation(summary = "根据文章ID查询评论（无分页）", description = "根据 articleId 查询该文章下的所有评论，不分页返回")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "未找到评论"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping("/article/{articleId}/all")
    public Result<?> getAllCommentsByArticleId(
            @Parameter(description = "文章ID", required = true)
            @PathVariable String articleId) {

        return commentService.findByPostId(articleId);
    }
    @Operation(summary = "删除指定文章的所有评论", description = "根据传入的 articleId 删除该文章下的所有评论记录")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "删除成功"),
            @ApiResponse(responseCode = "400", description = "参数错误"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @DeleteMapping("/article/{articleId}")
    public Result<?> deleteCommentsByArticleId(
            @Parameter(description = "文章ID", required = true)
            @PathVariable String articleId) {

        commentService.deleteCommentsByArticleId(articleId);
        return Result.success("已删除该文章下的所有评论");
    }


}
